//
//  ChangePWViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/9.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "ChangePWViewController.h"
#import "ChangePWItem.h"

@interface ChangePWViewController ()

@property (nonatomic, strong) ChangePWItem *item1;
@property (nonatomic, strong) ChangePWItem *item2;
@property (nonatomic, strong) ChangePWItem *item3;

@end

@implementation ChangePWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorFromRGB(0xf5f5f5);
    [self loadBaseTitleLable:@"修改密码"];
    self.rightBtn.hidden = NO;
    [self.rightBtn setTitle:@"  保存  " forState:(UIControlStateNormal)];
    [self.rightBtn addTarget:self action:@selector(saveAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self setUpUI];
}
- (void)setUpUI
{
    ChangePWItem *item1 = [[ChangePWItem alloc] init];
    item1.textField.placeholder = @"输入密码";
    item1.textField.secureTextEntry = YES;
    [self.view addSubview:item1];
    [item1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(self.view).offset(rateHeight(20)+SafeAreaTopHeight);
        make.centerX.equalTo(self.view);
    }];
    self.item1 = item1;
    
    ChangePWItem *item2 = [[ChangePWItem alloc] init];
    item2.textField.placeholder = @"输入新密码(6-18位)";
    item2.textField.secureTextEntry = YES;
    [self.view addSubview:item2];
    [item2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(item1.mas_bottom).offset(rateHeight(50));
        make.centerX.equalTo(self.view);
    }];
    self.item2 = item2;
    
    ChangePWItem *item3 = [[ChangePWItem alloc] init];
    item3.textField.placeholder = @"确认新密码(6-18位)";
    item3.textField.secureTextEntry = YES;
    [self.view addSubview:item3];
    [item3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(660), rateHeight(90)));
        make.top.equalTo(item2.mas_bottom);
        make.centerX.equalTo(self.view);
    }];
    self.item3 = item3;
}
#pragma mark - 保存
- (void)saveAction
{
    if (self.item1.textField.text.length != 0 && self.item2.textField.text.length != 0 && self.item3.textField.text.length != 0) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking postWithURLString:[NSString stringWithFormat:@"%@/api/json/member/safe.ashx?action=editpass&companyid=%@&code=%@&timestamp=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp]] parameters:@{@"oldpass":self.item1.textField.text,@"newpass":self.item2.textField.text,@"confirmpass":self.item3.textField.text} success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            if (responseObject) {
                
                if ([responseObject[@"status"] intValue] == 1) {
                    [self.navigationController popViewControllerAnimated:YES];
                    [MBProgressHUD showSuccess:responseObject[@"returnMsg"]];
                } else {
                    [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
                }
            }
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
